﻿using System.Collections.Generic;
using Microsoft.Maps.MapControl;

namespace MyFloridaHouse.libSvOpenDistricts.MapTools
{
    //TODO: MapWellKnownText: THis entire class can be deleted, use the MapMultiShape class
    public static class MapWellKnownText
    {

        private static LocationCollection RemoveDuplicatePoints(LocationCollection locationCollection)
        {
            int ndx = 0;
            while (ndx < locationCollection.Count - 1)
            {
                if (locationCollection[ndx] == locationCollection[ndx + 1])
                {
                    locationCollection.RemoveAt(ndx);
                }
                else
                {
                    ndx++;
                }

            }
            return locationCollection;
        }

        // Create a well known text string from a list of lat/lon point strings
        public static string WktFromLocations(LocationCollection locationCollection)
        {
            locationCollection = RemoveDuplicatePoints(locationCollection);

            if (locationCollection == null)
            {
                return "";
            }
            else
            {
                if (locationCollection.Count == 0)
                {
                    return "";
                }
                else
                {

                    // wrap with the appropriate well known text type
                    if (locationCollection.Count == 1) 
                    {
                        return "POINT" + GetWktPointList(locationCollection);
                    }
                    else if (locationCollection.Count == 2)
                    {
                        // we know they are 2 points but not the same
                        return "LINESTRING" + GetWktPointList(locationCollection);
                    }
                    else if (locationCollection[0] != locationCollection[locationCollection.Count - 1])
                    {
                        // we know they are at least 3 points, where endpoints don't match
                        // linestrings are not closed circles
                        return "LINESTRING" + GetWktPointList(locationCollection);
                    }
                    else if (locationCollection.Count == 3)
                    {
                        // we know we have 3 points with first and last point identical
                        // lets remove the last point and return a linestring
                        locationCollection.RemoveAt(2);
                        return "LINESTRING" + GetWktPointList(locationCollection);
                    }
                    else
                    {

                        return "POLYGON (" + GetWktPointList(locationCollection) + ")";
                    }
                }
            }
        }
        
        // Create a well known text string from a list of lat/lon point strings
        private static string GetWktPointList(LocationCollection locationCollection)
        {
            List<string> pointList = new List<string>();
            foreach (var newPoint in locationCollection)
            {
                pointList.Add(newPoint.Longitude + " " + newPoint.Latitude);
            }
            return  "(" + string.Join(",", pointList) + ")" ;
        }

    }
}
